package com.jeeplus.modules.web.wx;

import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.web.entity.YjDistrict;
import com.jeeplus.modules.web.service.YjDistrictService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 十戒丶
 * @date 2021-01-26 21:46:54
 */
@Controller
@RequestMapping(value = "${adminPath}/wx/district")
public class YjDistrictController extends BaseController {
    @Autowired
    private YjDistrictService yjDistrictService;
    
    @RequiresPermissions("wx:district:list")
    @RequestMapping(value = {"list", ""})
    public String list(Model model) {
        model.addAttribute("yjDistrict", new YjDistrict("0"));
        return "wx/district/list";
    }
    
    @ResponseBody
    @RequestMapping(value = "getChildren")
    public List<YjDistrict> getChildren(String parentId) {
        if ("-1".equals(parentId)) {
            parentId = "0";
        }
        return yjDistrictService.getChildren(parentId);
    }
    
    @RequiresPermissions(value = {"wx:district:view", "wx:district:add", "wx:district:edit"}, logical = Logical.OR)
    @RequestMapping(value = "form")
    public String form(YjDistrict yjDistrict, Model model) {
        if (yjDistrict.getParentid() == null || yjDistrict.getId() == null) {
            yjDistrict.setParentid("0");
        }
        yjDistrict.setParent(yjDistrictService.getDistrict(yjDistrict.getParentid()));
        model.addAttribute("yjDistrict", yjDistrict);
        return "wx/district/form";
    }
}
